﻿#define _CRT_SECURE_NO_WARNINGS 1

#include "Contact.h"
#include "SList.h"

//读通讯录中的数据
void LoadContact(Contact** con)
{
	FILE* pf = fopen("Contact.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadContact");
		return;
	}
	//循环读取文件数据 
	PeoInfo info;
	while (fread(&info, sizeof(PeoInfo), 1, pf))
	{
		SLTPushBack(con, info);
	}
	fclose(pf);
	pf = NULL;
	printf("历史数据导入通讯录成功！\n");
}
//初始化通讯录
void InitContact(Contact** con)
{
	LoadContact(con);//把本地的通讯录数据导⼊到链表结构 
}
//添加
void AddContact(Contact** con)
{
	PeoInfo info;
	printf("请输入要添加的联系人姓名:");
	scanf("%s", info.name);
	printf("请输入要添加的联系人性别:");
	scanf("%s", info.sex);
	printf("请输入要添加的联系人年龄:");
	scanf("%d", &info.age);
	printf("请输入要添加的联系人电话:");
	scanf("%s", info.tel);
	printf("请输入要添加的联系人地址:");
	scanf("%s", info.addr);

	SLTPushBack(con, info);
	printf("添加成功!\n");
}
Contact* FindName(Contact* con, char name[])
{
	Contact* cur = con;
	while (cur)
	{
		if (strcmp(cur->data.name, name) == 0)
		{
			return cur;
		}
		cur = cur->next;
	}
	//如果找不到
	return NULL;
}
//删除
void DelContact(Contact** con)
{
	char name[NAME_MAX];
	printf("请输入要删除的联系人姓名:");
	scanf("%s", name);

	Contact* pos = FindName(*con, name);
	if (pos == NULL)
	{
		printf("要删除的用户不存在，删除失败！\n");
		return;
	}
	else
	{
		SLTErase(con, pos);
	}
	printf("删除成功！\n");
}
//展示
void ShowContact(Contact* con)
{
	printf("-------------------------------显示结果-----------------------------------\n");
	printf("%-20s\t%-5s\t%-4s\t%-12s\t%-20s\n", "名字", "性别", "年龄", "电话", "地址");
	Contact* cur = con;
	while (cur)
	{
		printf("%-20s\t%-5s\t%-4d\t%-12s\t%-20s\n", cur->data.name, cur->data.sex, cur->data.age, cur->data.tel, cur->data.addr);
		cur = cur->next;
	}
	printf("-------------------------------显示结果-----------------------------------\n");
}
//查找
void FindContact(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名:");
	scanf("%s", name);

	Contact* pos = FindName(con, name);
	if (pos == NULL)
	{
		printf("要查找的用户不存在，查找失败！\n");
		return;
	}
	else
	{
		printf("查找成功！\n");
		printf("-------------------------------查找结果-----------------------------------\n");
		printf("%-20s\t%-4s\t%-5s\t%-20s\t%-12s\n", "名字", "年龄", "性别", "地址", "电话");
		printf("%-20s\t%-5s\t%-4d\t%-12s\t%-20s\n", pos->data.name, pos->data.sex, pos->data.age, pos->data.tel, pos->data.addr);
		printf("--------------------------------------------------------------------------\n");
	}
}
//修改
void ModifyContact(Contact** con)
{
	char name[NAME_MAX];
	printf("请输入要修改的联系人姓名:");
	scanf("%s", name);

	Contact* pos = FindName(*con, name);
	if (pos == NULL)
	{
		printf("要查找的用户不存在，修改失败！\n");
		return;
	}
	printf("请输入要修改的联系人姓名:");
	scanf("%s", pos->data.name);
	printf("请输入要修改的联系人性别:");
	scanf("%s", pos->data.sex);
	printf("请输入要修改的联系人年龄:");
	scanf("%d", &pos->data.age);
	printf("请输入要修改的联系人电话:");
	scanf("%s", pos->data.tel);
	printf("请输入要修改的联系人地址:");
	scanf("%s", pos->data.addr);

	printf("修改成功!\n");
}
//保存通讯录
void SaveContact(Contact* con)
{
	FILE* pf = fopen("Contact.txt", "wb");
	if (pf == NULL)
	{
		perror("SaveContact");
		return;
	}
	//将通讯录数据写入文件 
	Contact* cur = con;
	while (cur)
	{
		fwrite(&(cur->data), sizeof(cur->data), 1, pf);
		cur = cur->next;
	}
	fclose(pf);
	pf = NULL;
}
//销毁
void DestroyContact(Contact** con)
{
	SaveContact(*con);//在通讯录销毁之前，先把历史数据保存到本地⽂件中contact.txt
	SListDesTory(con);
}